<template>
  <view class="content">
    <details-top :title="`${labelName}详情`" :list="listData">
      <view class="elasticity-box">
        <view class="btn" @click="goPortrait"> 荣耀画像</view>
        <view class="btn" @click="$unity3dGlobal"> 元宇宙空间</view>
        <!-- <view class="btn " @click="onFriend"> 加为好友 </view> -->
      </view>
    </details-top>

    <view class="honour-box">
      <view class="honour-box-title">
        {{ honourCount }}项企业荣誉
      </view>
      <my-tab :show-badge="true" :tabBars="honourTabBars" :current="tabActive" @on-current-tab="onCurrentTab">
        <up-empty
            v-if="tagLabelData.length === 0"
            mode="data"
            text="企业暂无该级别荣誉"
            textSize="28rpx"/>
        <tag v-else :label-data="tagLabelData"/>
      </my-tab>
    </view>

    <view class="box">
      <text class="header-title min">基本情况</text>
      <VcCardBox :list="cardData"></VcCardBox>
      <view class="basic"> {{ businessScope }}</view>
    </view>
    <view class="box" style="margin-top: 30rpx">
      <text class="header-title min">{{ labelName }}介绍</text>
      <view class="introduce">
        {{ enterpriseCulture }}
      </view>
    </view>
    <view class="box" style="margin-top: 30rpx">
      <text class="header-title min">主营业务</text>
      <view class="introduce">
        {{ mainBusiness }}
      </view>
    </view>
    <view class="box" style="margin: 30rpx 0">
      <text class="header-title min">团队介绍</text>
      <view class="introduce">
        {{ teamIntroduction }}
      </view>
    </view>
    <view class="box" style="margin: 30rpx 0">
      <text class="header-title min">{{ labelName }}文化</text>
      <view class="introduce">
        {{ enterpriseCulture }}
      </view>
    </view>
  </view>
</template>

<script setup>
import detailsTop from "./components/details-top.vue";
import VcCardBox from "./components/card.vue";
import {
  ref
} from "vue";
import tag from "./components/tag.vue";
import {
  getAppEnterpriseById,
  getCollectiveInfoById
} from "@/api/honour/index.js";
import {
  onLoad
} from "@dcloudio/uni-app";
import {
  onMounted
} from "vue";
import MyTab from "@/components/tabsCopy/index.vue";
import {getAppEnterpriseByIdApi} from "@/api/honour/enterprise-detail";
import {getUrlParamsInH5, getUrlParamsInWeixin} from "@/utils/utils";
import {customNav} from "@/utils/customNav";

const businessScope = ref();
const mainBusiness = ref();

const labelName = ref("");
const listData = ref();
const teamIntroduction = ref("暂无")
const enterpriseCulture = ref('暂无')
const cardData = ref([
  {
    label: "法定代表人",
    value: "刘南余",
    type: "name",
    unit: "",
  },
  {
    label: "注册资本",
    value: "1138",
    type: "num",
  },
  {
    label: "成立日期",
    value: "2017.03.31",
    type: "data",
    unit: "",
  },
  {
    label: "企业规模",
    value: "中型",
    type: "name",
    unit: "",
  },
  {
    label: "行业类型",
    value: "软件开发",
    type: "name",
    unit: "",
  },
  {
    label: "所属区县",
    value: "72",
    type: "num",
  },
]);
const labelData = ref([
  "贵阳市市级C类人才",
  "贵阳市市级劳动模范",
  "中国青年科技进步奖获得者",
  "贵州省高层次人才引进计划",
  "贵州省大数据局特聘专家",
]);

function goPortrait() {
  customNav(`/package_honour/pages/honour/portrait?title=荣耀画像&industryBigType=${listData.value.name}&id=${listData.value.id}`)
}
const honourLevelHashMap = {
  '国际级': 'internationalLevel',
  '国家级': 'nationalLevel',
  '省部级': 'provincialLevel',
  '市级': 'prefectureLevel',
  '区县级': 'countyLevel',
  '自述级': 'otherLevel'
}
const honourLevelTagList = ref([])
const honourTabBars = ref([
  {
    name: "国际级",
    id: 0,
    key: "internationalLevel"
  },
  {
    name: "国家级",
    id: 1,
    key: "nationalLevel"
  },
  {
    name: "省部级",
    id: 2,
    key: "provincialLevel"
  },
  {
    name: "市级",
    id: 3,
    key: "prefectureLevel"
  },
  {
    name: "区县级",
    id: 4,
    key: "countyLevel"
  },
])
const honourCount = ref(0)
const tabActive = ref(0);
const tagLabelData = ref([])
const onCurrentTab = curTabIndex => {
  const curTab = honourTabBars.value.find(item => item.id === curTabIndex)
  tagLabelData.value = honourLevelTagList.value.find(item => item.label === curTab.key).tagList
}

const enterpriseId = ref('')
const getHonourTags = async () => {
  const params = {
    enterpriseId: enterpriseId.value
  }
  const res = await getAppEnterpriseByIdApi(params)
  const {code, data} = res
  if (code === 200) {
    const {enterpriseHonorGroupGardeMap} = data || {}
    Object.values(enterpriseHonorGroupGardeMap).forEach(item => {
      honourCount.value += item.length
    })
    const convertList = Object.keys(enterpriseHonorGroupGardeMap).map(key => {
      return {
        label: honourLevelHashMap[key],
        tagList: enterpriseHonorGroupGardeMap[key]
      }
    })
    const tempList = convertList.filter(item => item.tagList.length > 0)
    for (let i = 0; i < tempList.length; i++) {
      tempList[i].tagList.forEach((item,index) => {
        tempList[i].tagList[index] = {
          honorName: item,
          isExist: 1
        }
      })
    }
    for(let i = 0; i< convertList.length; i++) {
      for (let j = 0; j< tempList.length; j++) {
        if (convertList[i].label === tempList[j].label) {
          convertList[i].tagList = tempList[j].tagList
        }
      }
    }
    for (let i = 0; i< honourTabBars.value.length; i++) {
      for (let j = 0; j < convertList.length; j++) {
        if (honourTabBars.value[i].key === convertList[j].label) {
          honourTabBars.value[i].honourCount = convertList[j].tagList.length
        }
      }
    }
    tagLabelData.value = convertList.find(item => item.label === 'internationalLevel').tagList
    honourLevelTagList.value = convertList
  }
}
onMounted(() => {
  uni.setNavigationBarTitle({
    title: `${labelName.value}详情`,
  });
});
onLoad((option) => {
  uni.pageScrollTo({
    scrollTop: 0,
    duration: 0
  })
  /* #ifndef MP-WEIXIN */
  enterpriseId.value = getUrlParamsInH5()?.id
  /* #endif */

  /* #ifdef MP-WEIXIN */
  enterpriseId.value = getUrlParamsInWeixin()?.id
  /* #endif */
  labelName.value = option.typeName;
  getHonourTags()
  if (option.typeName == '企业') {
    getAppEnterpriseById(option.id).then((res) => {
      let records = res.data;
      listData.value = {
        name: records.enterpriseName,
        state: records.registerStatus,
        label: records.enterpriseHonorInfoList ? records.enterpriseHonorInfoList.map((o) => {
          return o.enterpriseHonor
        }) : [],
        stage: records.scCode,
        phone: records.phone,
        address: records.address,
        id: records.registerNumber
      };
      teamIntroduction.value = records.teamIntroduction;
      mainBusiness.value = records.mainBusiness;
      cardData.value[0].value = records.legalRepresentative;
      cardData.value[1].value = records.capital;
      cardData.value[2].value = records.businessDate;
      cardData.value[3].value = records.enterpriseScale;
      cardData.value[4].value = records.industryBigType;
      cardData.value[5].value = records.counties;
      businessScope.value = records.businessScope;
      enterpriseCulture.value = records.enterpriseIntroductory;
      labelData.value = records.enterpriseHonorInfoList ? records.enterpriseHonorInfoList.map((
          o) => {
        return o.enterpriseHonor
      }) : []

    })
  } else {
    getCollectiveInfoById(option.id).then((res) => {
      let records = res.data;
      listData.value = {
        name: records.collectiveName,
        state: records.registerStatus,
        label: records.enterpriseHonorInfoList ? records.enterpriseHonorInfoList.map((o) => {
          return o.enterpriseHonor
        }) : [],
        stage: records.scCode,
        phone: records.phone,
        address: records.address,
        id: records.registerNumber
      };
      teamIntroduction.value = records.teamIntroduction;
      mainBusiness.value = records.mainBusiness;
      cardData.value[0].value = records.legalRepresentative;
      cardData.value[1].value = records.capital;
      cardData.value[2].value = records.businessDate;
      cardData.value[3].value = records.collectiveScale;
      cardData.value[4].value = records.industryBigType;
      cardData.value[5].value = records.counties;
      businessScope.value = records.businessScope;
      enterpriseCulture.value = records.enterpriseIntroductory;
      labelData.value = records.enterpriseHonorInfoList ? records.enterpriseHonorInfoList.map((
          o) => {
        return o.enterpriseHonor
      }) : []

    })
  }

});
</script>

<style lang="less" scoped>
.honour-box {
  padding: 38rpx 34rpx 48rpx 34rpx;
  background-color: #fff;
  &-title {
    font-family: 'ali-p';
    font-weight: 500;
    color: #313233;
    font-size: 31rpx;
    letter-spacing: 1rpx;
  }
  :deep(.swiper-box) {
    margin-top: 40rpx;
    max-height: 200rpx;
    .uni-scroll-view-content {

    }
  }
}
.box {
  margin-top: 36rpx;

  .basic {
    font-family: "ali";
    font-weight: 400;
    font-size: @font-size-23;
    color: @color-black;
  }

  .introduce {
    font-family: "ali";
    font-weight: 400;
    font-size: @font-size-23;
    color: @color-label;
  }

  .elasticity-box {
    display: flex;
    flex-flow: row nowrap;
    margin-top: 15rpx;
    justify-content: flex-start;
    height: 60rpx;

    .btn {
      width: 120rpx;
      height: 52rpx;
      background: @bg-color;
      text-align: center;
      line-height: 52rpx;
      margin-right: 20rpx;
      padding: 0 20rpx;
      color: #5590f6;
    }
  }
}
</style>
